package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.dict.AssetsShowType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.VoiceRecord;
import com.ruoyi.ora.service.IVoiceRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.VoicePermission;
import com.ruoyi.ora.service.IVoicePermissionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 录音权限Controller
 * 
 * @author liuwp
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/ora/voice_permission")
public class VoicePermissionController extends BaseController
{
    @Autowired
    private IVoicePermissionService voicePermissionService;
    @Autowired
    private IVoiceRecordService voiceRecordService;

    /**
     * 查询录音权限列表
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:list')")
    @GetMapping("/list")
    public TableDataInfo list(VoicePermission voicePermission)
    {
        startPage();
        List<VoicePermission> list = voicePermissionService.selectVoicePermissionList(voicePermission);
        return getDataTable(list);
    }

    /**
     * 导出录音权限列表
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:export')")
    @Log(title = "录音权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoicePermission voicePermission)
    {
        List<VoicePermission> list = voicePermissionService.selectVoicePermissionList(voicePermission);
        ExcelUtil<VoicePermission> util = new ExcelUtil<VoicePermission>(VoicePermission.class);
        util.exportExcel(response, list, "录音权限数据");
    }

    /**
     * 获取录音权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(voicePermissionService.selectVoicePermissionById(id));
    }

    /**
     * 新增录音权限
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:add')")
    @Log(title = "录音权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VoicePermission voicePermission)
    {
        return toAjax(voicePermissionService.insertVoicePermission(voicePermission));
    }

    /**
     * 修改录音权限
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:edit')")
    @Log(title = "录音权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VoicePermission voicePermission)
    {
        return toAjax(voicePermissionService.updateVoicePermission(voicePermission));
    }

    /**
     * 删除录音权限
     */
    @PreAuthorize("@ss.hasPermi('ora:voice_permission:remove')")
    @Log(title = "录音权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(voicePermissionService.deleteVoicePermissionByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('ora:voice_permission:add')")
    @Log(title = "录音权限", businessType = BusinessType.INSERT)
    @PostMapping("/voiceGrant")
    public AjaxResult voiceGrant(VoicePermission voicePermission){
        if (voicePermission.getShowType() != null && voicePermission.getVoiceRecordId() != null){
            voiceRecordService.updateVoiceRecord(Builder.of(VoiceRecord::new)
                    .with(VoiceRecord::setId, voicePermission.getVoiceRecordId())
                    .with(VoiceRecord::setShowType, voicePermission.getShowType())
                    .build());
            if (voicePermission.getShowType().intValue() == AssetsShowType.PARTABLE.getValue().intValue()){
                if (voicePermission.getDeviceUserIds().length > 0){
                    // 删除原权限
                    voicePermissionService.deleteVoicePermissionByCondition(Builder.of(VoicePermission::new).with(VoicePermission::setVoiceRecordId, voicePermission.getVoiceRecordId()).build());
                    // 插入新权限 因为家庭圈不会有太多人就循环了
                    for (Long deviceUserId : voicePermission.getDeviceUserIds()) {
                        voicePermissionService.insertVoicePermission(Builder.of(VoicePermission::new)
                                .with(VoicePermission::setVoiceRecordId, voicePermission.getVoiceRecordId())
                                .with(VoicePermission::setDeviceUserId, deviceUserId)
                                .build());
                    }
                    return AjaxResult.success();
                }else {
                    return AjaxResult.error(MessageUtils.message("ora.common.required"));
                }
            }
            return AjaxResult.success();
        }else {
            return AjaxResult.error(MessageUtils.message("ora.common.required"));
        }
    }

}
